<?php

/**
 * Project: Espolon
 * Licence: BSD License
 * Link: http://espolon.org/
 * Copyright: Nicolas de Bari Embriz Garcia Rojas <nbari at espolon dot org>
 * Author: Nicolas de Bari Embriz Garcia Rojas <nbari at espolon dot org>
 * Version: Beta
 */

if (!defined('SITE_ROOT')) exit('No direct script access allowed');

$command = isset($options[1]) ? $options[1] : null;
$user = new Users();

switch ($command){
	case 'add':
	  $form = new AddUser($tpl, $user);
    if ($form->validate()) {
      $form->freeze();
      $form->process();
    } else {
      $form->displayForm();
    }
	  $tpl->assign('tab2', true);
    $option_tpl = 'add_user.tpl';
	break;
	case 'delete':
		$user->setUid((isset($options[2]) AND is_number($options[2])) ? $options[2] : sign_out());

    $tpl->assign('tab1', true);
    $tpl->assign('login', $user->getUserData('email'));
    $option_tpl = 'delete_user.tpl';

    if (isset($options[3]) AND $options[3] == 'true') {
      $user->deleteUser();
      $tpl->assign('result',true);
    }
	break;
	case 'modify':
		$user->setUid((isset($options[2]) AND is_number($options[2])) ? $options[2] : sign_out());
    $form = new ModifyUser($tpl, $user);
    if ($form->validate()) {
      $form->freeze();
      $form->process();
      $db->CacheFlush();
      } else {
        $form->displayForm();
      }
	  $tpl->assign('tab1', true);
	  $option_tpl = 'modify_user.tpl';
	break;
	default:
	  $tpl->assign('tab1', true);
	  $option_tpl = 'list_users.tpl';
	  $users = $db->GetAll("SELECT uid, email, created FROM core_users ORDER by uid DESC");
	  $tpl->assign('msg', $db->ErrorMsg());
	  $tpl->assign('users', $users);
}

$tpl->assign('option_tpl',"$template/modules/$module/$option_tpl");
/**
 * show the errors
 */
if ($user->getMsg()) {
  $tpl->assign('msg', $user->getMsg());
}

?>